import Vue from 'vue'
import VueRouter from 'vue-router'

import Index from '../views/Index/Index.vue'

Vue.use(VueRouter)

// 点击相同的routerPath会导致报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/w',
    component: () => import('../components/common/Wrapper')
  },
  {
    path: '/home',
    //  在import里面是不可以有空格的.
    component: () => import('../views/Home.vue'),
    children: [
      {
        // 首页的路由
        path: '/index',
        // 首页不用进行懒加载
        component: Index
      },
      {
        // 分类的路由
        path: '/cate',
        component: () => import('../views/cate/Cate.vue')
      },
      {
        // 购物车路由
        path: '/cart',
        component: () => import('../views/cart/Cart.vue')
      },
      {
        // 我的路由
        path: '/profile',
        component: () => import('../views/profile/Profile.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes,
  // 使用history模式
  mode: 'history'
})

export default router
